from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession

from app.database.models import Settings


async def get_soft_status(session: AsyncSession, telegram_id: int) -> int:
    q = select(Settings.status).filter(Settings.chat_id == telegram_id)
    res = await session.execute(q)

    return res.scalar()


async def create_user_settings(session, telegram_id: int) -> None:
    settings = Settings(chat_id=telegram_id)

    session.add(settings)

    await session.commit()


async def change_soft_status(
    session: AsyncSession, telegram_id: int, status: int
) -> None:
    q = update(Settings).filter(Settings.chat_id == telegram_id).values(status=status)
    await session.execute(q)
    await session.commit()


async def get_logging_status(session: AsyncSession, telegram_id: int) -> int:
    q = select(Settings.logging_status).filter(Settings.chat_id == telegram_id)
    res = await session.execute(q)

    return res.scalar()


async def change_logging_status(
    session: AsyncSession, telegram_id: int, status: int
) -> None:
    q = (
        update(Settings)
        .filter(Settings.chat_id == telegram_id)
        .values(logging_status=status)
    )
    await session.execute(q)
    await session.commit()


async def get_user_settings(session: AsyncSession, telegram_id: int) -> Settings:
    q = select(Settings).filter(Settings.chat_id == telegram_id)
    res = await session.execute(q)

    return res.scalar()


async def get_links(session: AsyncSession, telegram_id: int) -> str:
    q = select(Settings.links).filter(Settings.chat_id == telegram_id)
    res = await session.execute(q)

    return res.scalar()


async def add_links(session: AsyncSession, telegram_id: int, links: str) -> None:
    q = update(Settings).filter(Settings.chat_id == telegram_id).values(links=links)
    await session.execute(q)

    await session.commit()
